<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;
header("Content-type: text/html; charset=utf-8");
/**
 * 教务控制器
 */
class EducationalController extends HomeController
{

    /**
     * 检测是否登录
     * */
    public function _initialize()
    {
        if(empty(session('user_id'))){
            header('Location: ' . U('/Home/Login/Login'));exit;
        }else{
            //再来判断权限
            if(session('Supertube')==1){
                //超管 拥有所有权限
            }else{
                $id=session('user_id');//不是超管的时候 user_id是account_number的主键ID
                $jurisdiction=M('account_number')->where("id=$id")->getField('jurisdiction');
                $action_name=ACTION_NAME;
                if($action_name == learning_statistics || $action_name == Learning_statistics){
                    //访问数据统计
                    $domain = strstr($jurisdiction, ',6,');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }elseif($action_name == studentList || $action_name == StudentList){
                    //排行榜
                    $domain = strstr($jurisdiction, ',7,');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }elseif($action_name == classClassification || $action_name == ClassClassification){
                    //线索列表
                    $domain = strstr($jurisdiction, ',8,');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }elseif($action_name == classList || $action_name == ClassList){
                    //客户列表
                    $domain = strstr($jurisdiction, ',9,');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }elseif($action_name == online_results || $action_name == Online_results){
                    //跟进记录
                    $domain = strstr($jurisdiction, ',10,');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }elseif($action_name == wrong_questions || $action_name == Wrong_questions){
                    //跟进记录
                    $domain = strstr($jurisdiction, ',11,');
                    if(!$domain){
                        $this->error('没有访问权限');
                    }
                }
            }
        }
    }

    /**
     * Mr.Jiang
     * 班级分类
     */
    public function classClassification()
    {
        $iid = session('iid');
        $map['cc_id'] = array('eq',0);
        $map['iid'] = array('eq',$iid);
        $list = D('class_classification')->where($map)->order('sort asc')->select();
        foreach ($list as $key=>$item) {
            $child = D('class_classification')->where(array('cc_id'=>$item['id']))->order('sort asc')->select();
            $list[$key]['child'] = $child?$child:[];
        }
        $this->assign('_list', $list);
        $this->assign('cur','班级分类');
        $this->display();
    }

    /**
     * Mr.Jiang
     * 新增或修改班级分类
     */
    public function saveClassClassification()
    {
        $id = $_REQUEST['id'];
        $data['iid'] = session('iid');
        $data['cc_id'] = $_REQUEST['cc_id']?$_REQUEST['cc_id']:0;
        $data['name'] = $_REQUEST['name'];
        $data['sort'] = $_REQUEST['sort'];

        if($data['name'] == ''){
            return api_return(2, '请输入类目名称');
        }

        $time = time();
        $data['update_time'] = $time;
        if(empty($id)){
            $data['create_time'] = $time;
            $result = D('class_classification')->add($data);
            if($result){
                if(session('Supertube')==2){
                    //不记录超管的 只记录这种有权限的
                    $data['account_id']=session('user_id');
                    $data['create_time']=time();
                    $data['ip_address']=$_SERVER["REMOTE_ADDR"];
                    $data['content']='班级分类'.'-'.'新增分类';
                    M('account_log')->add($data);
                }
                return api_return(1, '新增成功');
            }else {
                return api_return(2, '新增失败');
            }
        }else {
            $result = D('class_classification')->where(array('id'=>$id))->save($data);
            if($result){
                if(session('Supertube')==2){
                    //不记录超管的 只记录这种有权限的
                    $data['account_id']=session('user_id');
                    $data['create_time']=time();
                    $data['ip_address']=$_SERVER["REMOTE_ADDR"];
                    $data['content']='班级分类'.'-'.'编辑分类';
                    M('account_log')->add($data);
                }
                return api_return(1, '编辑成功');
            }else {
                return api_return(2, '编辑失败');
            }
        }
    }

    /**
     * Mr.Jiang
     * 删除班级分类
     */
    public function delClassClassification()
    {
        $id = $_REQUEST['id'];
        $count = D('class_classification')->where(array('id' => $id))->count();
        if($count <= 0){
            $this->error("当前类目不存在或已被删除");
        }

        $cc_id_count = D('class_classification')->where(array('cc_id'=>$id))->count();
        if($cc_id_count > 0){
            $this->error("抱歉，请先删除子类目");
        }

        $result = D('class_classification')->where(array('id' => $id))->delete();
        if($result){
            $this->success("删除成功");
        }else {
            $this->error("删除失败");
        }
    }

    /**
     * Mr.Jiang
     * 班级列表
     */
    public function classList()
    {
        $iid = session('iid');
        $map['iid'] = array('eq',$iid);
        if($_REQUEST['name'] != ''){
            $map['name'] = array('like','%'.$_REQUEST['name'].'%');
            $this->assign('name',$_REQUEST['name']);
        }

        if(!empty($_REQUEST['one_class_id'])){
            $map['one_class_id'] = array('eq',$_REQUEST['one_class_id']);
            $this->assign('one_class_id',$_REQUEST['one_class_id']);
        }

        if(!empty($_REQUEST['two_class_id'])){
            $map['two_class_id'] = array('eq',$_REQUEST['two_class_id']);
            $this->assign('two_class_id',$_REQUEST['two_class_id']);
        }

        if(!empty($_REQUEST['status'])){
            $map['status'] = array('eq',$_REQUEST['status']);
            $this->assign('status',$_REQUEST['status']);
        }

       /* if(!empty($_REQUEST['start_time'])){
            $map['start_time'] = array('eq',$_REQUEST['start_time']);
            $this->assign('start_time',$_REQUEST['start_time']);
        }

        if(!empty($_REQUEST['end_time'])){
            $map['end_time'] = array('eq',$_REQUEST['end_time']);
            $this->assign('end_time',$_REQUEST['end_time']);
        }*/

        $count = D('class')->where($map)->count();
        $Page = new \Think\Page($count, 10, array('name'=>$_REQUEST['name'],'one_class_id'=>$_REQUEST['one_class_id'],'two_class_id'=>$_REQUEST['two_class_id'],'status'=>$_REQUEST['status']));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list = D('class')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('create_time desc')->select();
        foreach ($list as $key=>$item) {
            $parent = D('class_classification')->where(array('id'=>$item['one_class_id']))->getField('name');
            $child = D('class_classification')->where(array('id'=>$item['two_class_id']))->getField('name');
            $list[$key]['parent'] = $parent?$parent:'';
            $list[$key]['child'] = $child?$child:'';

            $count = D('ucenter_member')->where(array('user_type'=>2,'class_id'=>$item['id']))->count();
            $list[$key]['count'] = $count?$count:0;
        }

        $this->assign('_list', $list);
        $show = $Page->show();// 分页显示输出
        $this->assign("_page", $show);

        $parent = D('class_classification')->where(array('iid'=>$iid,'cc_id'=>0))->order('sort asc')->select();
        if(!empty($_REQUEST['one_class_id'])){
            $child = D('class_classification')->where(array('iid'=>$iid,'cc_id'=>$_REQUEST['one_class_id']))->order('sort asc')->select();
        }else {
            $child = D('class_classification')->where(array('iid'=>$iid,'cc_id'=>$parent[0]['id']))->order('sort asc')->select();
        }
        $this->assign('parent',$parent);
        $this->assign('child',$child);

        $this->assign('cur','班级列表');
        $this->display();
    }

    /**
     * Mr.Jiang
     * 新增或修改班级
     */
    public function saveClass()
    {
        if(IS_POST){
            $id = $_REQUEST['id'];
            $data['iid'] = session('iid');
            $data['name'] = $_REQUEST['name'];
            $data['one_class_id'] = $_REQUEST['one_class_id'];
            $data['two_class_id'] = $_REQUEST['two_class_id'];
            $data['status'] = $_REQUEST['status'];
            $data['start_time'] = strtotime($_REQUEST['start_time']);
            $data['end_time'] = strtotime($_REQUEST['end_time']);

            if($data['name'] == ''){
                $this->error("请输入班级名称");
            }

            if(empty($data['one_class_id'])){
                $this->error("请选择一级分类");
            }

            if(empty($data['two_class_id'])){
                $this->error("请选择二级分类");
            }

            if(empty($data['start_time'])){
                $this->error("请选择开班时间");
            }

            if(empty($data['end_time'])){
                $this->error("请选择结束时间");
            }

            if($data['start_time'] > $data['end_time']){
                $this->error("结束时间不能小于开班时间");
            }

            $time = time();
            $data['update_time'] = $time;

            if(empty($id)){
                $data['create_time'] = $time;
                $result = D('class')->add($data);
                if($result){
                    if(session('Supertube')==2){
                        //不记录超管的 只记录这种有权限的
                        $data['account_id']=session('user_id');
                        $data['create_time']=time();
                        $data['ip_address']=$_SERVER["REMOTE_ADDR"];
                        $data['content']='班级列表'.'-'.'新增班级';
                        M('account_log')->add($data);
                    }
                    $this->success("新增成功",U('classList'));
                }else {
                    $this->error("新增失败");
                }
            }else {
                $result = D('class')->where(array('id'=>$id))->save($data);
                if($result){
                    if(session('Supertube')==2){
                        //不记录超管的 只记录这种有权限的
                        $data['account_id']=session('user_id');
                        $data['create_time']=time();
                        $data['ip_address']=$_SERVER["REMOTE_ADDR"];
                        $data['content']='班级列表'.'-'.'修改班级';
                        M('account_log')->add($data);
                    }
                    $this->success("编辑成功",U('classList'));
                }else {
                    $this->error("编辑失败");
                }
            }
        }else {
            $iid = session('iid');
            $id = $_REQUEST['id'];
            $info = D('class')->where(array('id'=>$id))->find();

            //一级分类
            $parent = D('class_classification')->where(array('iid'=>$iid,'cc_id'=>0))->order('sort asc')->select();

            //二级分类
            if(empty($info['one_class_id'])){
                $child = D('class_classification')->where(array('iid'=>$iid,'cc_id'=>$parent[0]['id']))->order('sort asc')->select();
            }else {
                $child = D('class_classification')->where(array('iid'=>$iid,'cc_id'=>$info['one_class_id']))->order('sort asc')->select();
            }


            if(empty($info['start_time'])){
                $info['start_time'] = '';
            }else {
                $info['start_time'] = date('Y-m-d',$info['start_time']);
            }

            if(empty($info['end_time'])){
                $info['end_time'] = '';
            }else {
                $info['end_time'] = date('Y-m-d',$info['end_time']);
            }
            $this->assign('info',$info);

            $this->assign('parent',$parent);
            $this->assign('child',$child);
            $this->assign('cur','班级列表');
            $this->display();
        }
    }

    /**
     * Mr.Jiang
     * 删除班级
     */
    public function delClass()
    {
        $id = $_REQUEST['id'];
        $count = D('class')->where(array('id'=>$id))->count();
        if($count <= 0){
            $this->error("当前数据不存在或已被删除");
        }

        $result = D('class')->where(array('id'=>$id))->delete();
        if($result){
            if(session('Supertube')==2){
                //不记录超管的 只记录这种有权限的
                $data['account_id']=session('user_id');
                $data['create_time']=time();
                $data['ip_address']=$_SERVER["REMOTE_ADDR"];
                $data['content']='班级列表'.'-'.'删除班级';
                M('account_log')->add($data);
            }
            $this->success("删除成功");
        }else {
            $this->error("删除失败");
        }
    }

    /**
     * Mr.Jiang
     * 根据一级分类获得班级二级分类
     */
    public function getTwoClass()
    {
        $iid = session('iid');
        $cc_id = $_REQUEST['cc_id'];
        $list = D('class_classification')->where(array('iid'=>$iid,'cc_id'=>$cc_id))->order('sort asc')->select();
        $list = $list?$list:[];
        return api_return(1, '信息获取成功',$list);
    }

    /**
     * Mr.Jiang
     * 学生管理
     */
    public function studentList()
    {
        // print_r($_REQUEST);die;
        $_REQUEST['type']?$type=$_REQUEST['type']:$type=1;
        $this->assign('type',$type);
        if($_REQUEST['start_time'] && $_REQUEST['end_time']){
            $btime=strtotime($_REQUEST['start_time']);
            $etime=strtotime($_REQUEST['end_time']." 23:59:59");
            $map['reg_time']=array('between',"$btime,$etime");  
        }
        $_REQUEST['username']?$map['username']=array('like',"%$_REQUEST[username]%"):'';
        $_REQUEST['class_id']?$map['class_id']=$_REQUEST['class_id']:'';
        $iid = session('iid');
        $map['iid'] = array('eq',$iid);
        // $map['user_type'] = array('eq',2);
        $map['user_type'] = array('like',"%2%");
        $count = D('ucenter_member')->where($map)->count();
        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        if($type==1){
            //注册时间排序
            $list = D('ucenter_member')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('reg_time desc')->select();
            foreach($list as $k=>$v){
                $list[$k]['c_lxc'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=1 && a.uid=$v[id]")->count();
                $list[$k]['c_order']=M('order')->where("uid=$v[id]")->count();
                $list[$k]['c_sj'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=2 && a.uid=$v[id]")->count();
            }
        }elseif($type==2){
            //购买课程数排序
            $list = D('ucenter_member')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
            foreach($list as $k=>$v){
                $list[$k]['c_lxc'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=1 && a.uid=$v[id]")->count();
                $list[$k]['c_order']=M('order')->where("uid=$v[id]")->count();
                $list[$k]['c_sj'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=2 && a.uid=$v[id]")->count();
            }
            foreach ($list as $key => $row) {
                $volume[$key] = $row['c_order'];
            }
            array_multisort($volume,SORT_DESC, $list);
        }elseif($type==3){
            //登录次数
            $list = D('ucenter_member')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('login_count desc')->select();
            foreach($list as $k=>$v){
                $list[$k]['c_lxc'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=1 && a.uid=$v[id]")->count();
                $list[$k]['c_order']=M('order')->where("uid=$v[id]")->count();
                $list[$k]['c_sj'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=2 && a.uid=$v[id]")->count();
            }
        }elseif($type==4){
            //做试卷数
            $list = D('ucenter_member')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
            foreach($list as $k=>$v){
                $list[$k]['c_lxc'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=1 && a.uid=$v[id]")->count();
                $list[$k]['c_order']=M('order')->where("uid=$v[id]")->count();
                $list[$k]['c_sj'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=2 && a.uid=$v[id]")->count();
            }

            foreach ($list as $key => $row) {
                $volume[$key] = $row['c_sj'];
            }


            // 将数据根据 volume 降序排列，根据 edition 升序排列
            // 把 $data 作为最后一个参数，以通用键排序
            array_multisort($volume,SORT_DESC, $list);
            
        }elseif($type==5){
            //做题数
            $list = D('ucenter_member')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
            foreach($list as $k=>$v){
                $list[$k]['c_lxc'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=1 && a.uid=$v[id]")->count();
                $list[$k]['c_order']=M('order')->where("uid=$v[id]")->count();
                $list[$k]['c_sj'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=2 && a.uid=$v[id]")->count();
            }

            foreach ($list as $key => $row) {
                $volume[$key] = $row['c_lxc'];
            }


            // 将数据根据 volume 降序排列，根据 edition 升序排列
            // 把 $data 作为最后一个参数，以通用键排序
            array_multisort($volume,SORT_DESC, $list);
        }elseif($type==6){
            //观看视频时长  all_study_time是总学习时长
            $list = D('ucenter_member')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('all_study_time desc')->select();
            foreach($list as $k=>$v){
                $list[$k]['c_lxc'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=1 && a.uid=$v[id]")->count();
                $list[$k]['c_order']=M('order')->where("uid=$v[id]")->count();
                $list[$k]['c_sj'] = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=2 && a.uid=$v[id]")->count();
            }
        }
        
        foreach ($list as $key=>$item) {
            $name = D('class')->where(array('id'=>$item['class_id']))->getField('name');
            $list[$key]['class_name'] = $name?$name:'暂无';
            $person_charge = D('ucenter_member')->where(array('id'=>$item['person_charge']))->getField('username');
            $list[$key]['person_charge'] = $person_charge?$person_charge:'';
        }

        //班级列表
        $class_list=M('class')->where("iid=$iid")->select();
        $this->assign('msg',$_REQUEST);
        $this->assign('class_list',$class_list);
        $this->assign('_list', $list);
        $show = $Page->show();// 分页显示输出
        $this->assign("_page", $show);
        $this->assign('cur','学生管理');
        $this->display();
    }

    //学生管理 详情
    public function study_detail(){
        $_REQUEST['type']?$type=$_REQUEST['type']:$type=1;
        $this->assign('type',$type);
        $id=$_REQUEST['id'];
        $info=M('ucenter_member')->where("id=$id")->find();
        // print_r($info);die;
        if($info['class_id']){
            $info['class_name']=M('class')->where("id=$info[class_id]")->getField('name');
        }else{
            $info['class_name']='';
        }
        
        if($info['source']==1){
            $info['source']='线上咨询';
        }elseif($info['source']==2){
            $info['source']='客户介绍';
        }elseif($info['source']==3){
            $info['source']='学生介绍';
        }elseif($info['source']==4){
            $info['source']='线下联系';
        }else{
            $info['source']='其他';
        }

            $count1=M('vice_order a')->join('left join teach_order b on a.order_id=b.id')->where("b.uid=$id && b.type=1")->field('a.*')->count();
            $count2= M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=2 && a.uid=$id")->field('b.name,a.create_time')->count();
            $count3 = M('vice_order a')->join('left join teach_order b on a.order_id=b.id')->where("b.uid=$id && b.type=3")->field('a.*')->count();
            $count4 = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=1 && a.uid=$id")->field('b.name,a.create_time')->count();                                    
        if($type==1){
            //购课 直播

            $list=M('vice_order a')->join('left join teach_order b on a.order_id=b.id')->where("b.uid=$id && b.type=1")->field('a.*')->select();
            foreach($list as $k=>$v){
                $list[$k]['name']=M('live_course')->where("id=$v[to_id]")->getField('name');
                $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                $list[$k]['class']='购课';
            }
        }elseif($type==2){
            //试卷

            $list = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=2 && a.uid=$id")->field('b.name,a.create_time')->select();
            foreach($list as $k=>$v){
                $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                $list[$k]['class']='试卷';
            }
        }elseif($type==3){
            //视频  点播课
            $list=M('vice_order a')->join('left join teach_order b on a.order_id=b.id')->where("b.uid=$id && b.type=3")->field('a.*')->select();
            foreach($list as $k=>$v){
                $list[$k]['name']=M('video_course')->where("id=$v[to_id]")->getField('name');
                $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                $list[$k]['class']='视频';
            }
            
        }elseif($type==4){
            //练习册

            $list = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where("b.type=1 && a.uid=$id")->field('b.name,a.create_time')->select();
            foreach($list as $k=>$v){
                $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                $list[$k]['class']='练习册';
            }
        }
        $this->assign('count1',$count1);
        $this->assign('count2',$count2);
        $this->assign('count3',$count3);
        $this->assign('count4',$count4);
        $this->assign('list',$list);
        $this->assign('id',$id);
        $this->assign('info',$info);
        $this->assign('cur','学生管理');
        $this->display();
    }
    /**
     * Mr.Jiang
     * 添加学生
     */
    public function addStudent()
    {
        if (IS_POST) {
            $data['iid'] = session('iid');
            $data['username'] = $_REQUEST['username'];
            $data['id_card'] = $_REQUEST['id_card'];
            $data['sex'] = $_REQUEST['sex'];
            $data['age'] = $_REQUEST['age'];
            $data['mobile'] = $_REQUEST['mobile'];
            $data['person_charge'] = $_REQUEST['person_charge'];
            $data['source'] = $_REQUEST['source'];

            if($data['username'] == ''){
                $this->error("请输入姓名");
            }

            if($data['id_card'] == ''){
                $this->error("请输入身份证号");
            }

            $id_card_count = D('ucenter_member')->where(array('id_card'=>$data['id_card']))->count();
            if($id_card_count > 0){
                $this->error("身份证号已存在");
            }

            if($data['mobile'] == ''){
                $this->error("请输入手机号");
            }

            $mobile_count = D('ucenter_member')->where(array('mobile'=>$data['mobile']))->count();
            if($mobile_count > 0){
                $this->error("手机号已存在");
            }

            $data['password'] = think_ucenter_md5('123456', '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT');
            $data['user_type'] = 2;
            $time = time();
            $data['reg_time'] = $time;
            $data['update_time'] = $time;
            $data['status'] = 1;
            $result = D('ucenter_member')->add($data);
            if ($result) {
                $member_data['nickname'] = $data['username'];
                $member_data['status'] = 1;
                D('member')->add($member_data);
                if(session('Supertube')==2){
                    //不记录超管的 只记录这种有权限的
                    $data['account_id']=session('user_id');
                    $data['create_time']=time();
                    $data['ip_address']=$_SERVER["REMOTE_ADDR"];
                    $data['content']='学生管理'.'-'.'添加学生';
                    M('account_log')->add($data);
                }
                $this->success("添加成功", U('studentList'));
            } else {
                $this->error("添加失败");
            }
        } else {
            //老师
            $iid = session('iid');
            $teach = D('ucenter_member')->where(array('iid'=>$iid, 'user_type' => 4))->order('id desc')->select();
            $this->assign('teach', $teach);
            $this->assign('cur','学生管理');
            $this->display();
        }
    }

    //删除学生
    public function student_del(){
        $id=$_REQUEST['id'];
        $res=M('ucenter_member')->where("id=$id")->delte();
        if($res){
            if(session('Supertube')==2){
                //不记录超管的 只记录这种有权限的
                $data['account_id']=session('user_id');
                $data['create_time']=time();
                $data['ip_address']=$_SERVER["REMOTE_ADDR"];
                $data['content']='学生管理'.'-'.'删除学生';
                M('account_log')->add($data);
            }
            M('member')->where("uid=$id")->delete();
            $this->success('成功',U('StudentList'));
        }else{
            $this->error('删除失败');
        }
    }
    /**
     * Mr.Jiang
     * 获取未分班的学生
     */
    public function getNotClassStudent()
    {
        $iid = session('iid');
        $map['iid'] = array('eq',$iid);
        $map['user_type'] = array('eq',2);
        $map['class_id'] = array('eq',0);
        $student = D('ucenter_member')->where($map)->order('reg_time desc')->select();
        $list['student'] = $student?$student:[];

        //一级分类
        $parent = D('class_classification')->where(array('cc_id'=>0,'iid'=>$iid))->order('sort asc')->select();
        //二级分类
        $child = D('class_classification')->where(array('cc_id'=>$parent[0]['id'],'iid'=>$iid))->order('sort asc')->select();
        //班级
        $class = D('class')->where(array('iid'=>$iid,'one_class_id'=>$parent[0]['id'],'two_class_id'=>$child[0]['id']))->order('create_time desc')->select();

        $list['parent'] = $parent?$parent:[];
        $list['child'] = $child?$child:[];
        $list['class'] = $class?$class:[];
        return api_return(1, '信息获取成功', $list);
    }

    /**
     * Mr.Jiang
     * 根据一级分类获得二级分类与班级
     */
    public function getTwoClassAndClass()
    {
        $iid = session('iid');
        $cc_id = $_REQUEST['cc_id'];
        $child = D('class_classification')->where(array('iid'=>$iid,'cc_id'=>$cc_id))->order('sort asc')->select();

        //班级
        $class = D('class')->where(array('iid'=>$iid,'one_class_id'=>$cc_id,'two_class_id'=>$child[0]['id']))->order('create_time desc')->select();

        $list['child'] = $child?$child:[];
        $list['class'] = $class?$class:[];
        return api_return(1, '信息获取成功',$list);
    }

    /**
     * Mr.Jiang
     * 根据一级和二级分类获得班级
     */
    public function getClass()
    {
        $iid = session('iid');
        $cc_id = $_REQUEST['cc_id'];
        $two_class_id = $_REQUEST['two_class_id'];
        $list = D('class')->where(array('iid'=>$iid,'one_class_id'=>$cc_id,'two_class_id'=>$two_class_id))->order('create_time desc')->select();
        $list = $list?$list:[];
        return api_return(1, '信息获取成功',$list);
    }

    /**
     * Mr.Jiang
     * 分班
     */
    public function division()
    {
        $ids = $_REQUEST['id'];
        $class_id = $_REQUEST['class_id'];
        if($ids == ''){
            return api_return(2, '没有待分班学生');
        }

        if($class_id == ''){
            return api_return(2, '请选择班级');
        }

        $id_arr = explode(",",$ids);
        for($i=0;$i<count($id_arr);$i++){
            $result = D('ucenter_member')->where(array('id'=>$id_arr[$i]))->save(array('class_id'=>$class_id));
        }
        return api_return(1, '分班成功');
    }
    //x学习统计
    public function Learning_statistics(){
        $iid=session('iid');
        
        $pass_count=0;//试卷及格数
        $write_count=0;//做试卷数量
        $list=M('practice')->field('pass_count,write_count')->select();
        foreach($list as $k=>$v){
            $pass_count+=$v['pass_count'];
            $write_count+=$v['write_count'];
        }
        //做习题数量
        $make_list=M('exercises_log')->select();
        $make_count=0;
        foreach($make_list as $k=>$v){
            $make_count+=$v['do_count'];
            $error_count+=$v['error_count'];
        }

        //习题总量
        $exercises_count=M('exercises')->count();
        $pass_rate=$pass_count/$write_count*100;
        $make_rate=$make_count/$exercises_count*100;
        $error_rate=$error_count/$exercises_count*100;
        //不及格率
        $fail_rate=100-$pass_rate;

        //试卷统计  图表分析
        $_REQUEST['name']?$map['b.name']=array('like',"%$_REQUEST[name]%"):'';
        $this->assign('name',$_REQUEST['name']);
        $start_time = strtotime($_REQUEST['start_time']);
        $end_time = strtotime($_REQUEST['end_time']." 23:59:59");
        if(!empty($_REQUEST['start_time']) && !empty($_REQUEST['end_time'])){
            //这个时间条件会被切割
            $this->assign('start_time',$_REQUEST['start_time']);
            $this->assign('end_time',$_REQUEST['end_time']);

                    //考试次数
        // M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->where($map)->count();

        //开始时间 ~ 结束时间
        $start_date=I('start_time');
        $end_date=I('end_time');
//
        //如果为空，则从今天的0点为开始时间
        if(!empty($start_date))
            $start_date=date('Y-m-d',strtotime($start_date));
        else
            $start_date=date('Y-m-d',time());
//
        //如果为空，则以明天的0点为结束时间（不存在24:00:00，只会有00:00:00）
        if(!empty($end_date))
            $end_date=date('Y-m-d',strtotime($end_date));
        else
            $end_date=date('Y-m-d',strtotime('+1 day'));

        //between 查询 要求必须是从低到高
        if($start_date>$end_date)
        {
            $ttt=$start_date;
            $start_date=$end_date;
            $end_date=$ttt;
        }elseif($start_date==$end_date){
            echo '时间输入错误';die;
        }
        $time_s=strtotime($start_date);
        $time_e=strtotime($end_date);

        //生成中间时间点数组（时间戳格式、日期时间格式、日期序列）
        $days_inline_array=array();
        $times_inline_array=array();
        $days_list=array();
        //判断开始和结束时间是不是在同一天
        $days_inline_array[0]=$start_date;
        $times_inline_array[0]=$time_s;
        if(
            date('Y-m-d',$time_s)
            ==date('Y-m-d',$time_e)
        ){
            $days_inline_array[1]=$end_date;
            $times_inline_array[1]=$time_e;
            $days_list[]=date('Y-m-d',$time_s);
        }
        else
        {
            /**
             * A.取开始时间的第二天凌晨0点
             * B.用结束时间减去A
             * C.用B除86400取商，取余
             * D.用A按C的商循环+86400，取得分割时间点，如果C没有余数，则最后一个时间点与循环最后一个时间点一致
             */
            $A_temp=date('Y-m-d',$time_s+86400);
            $A=strtotime($A_temp);
            $B=$time_e-$A;
            $C_quotient=floor($B/86400);    //商舍去法取整
            $C_remainder=fmod($B,86400);               //余数

            $days_inline_array[1]=$A_temp;
            $times_inline_array[1]=$A;
            $days_list[]=date('Y-m-d',$A);
            for($increase_time=$A,$c_count_t=1;$c_count_t<=$C_quotient;$c_count_t++)
            {
                $increase_time+=86400;
                $days_inline_array[]=date('Y-m-d',$increase_time);
                $times_inline_array[]=$increase_time;
                $days_list[]=date('Y-m-d',$increase_time);
            }
            //如果有余数，则最后一个时间点大于循环最后一个时间点，但在同一天
            if($C_remainder)
            {
                $days_inline_array[]=$end_date;
                $times_inline_array[]=$time_e;
                //这里不再添加 $days_list，因为余数和最后一个凌晨0点必定是同一天
            }
        }

        // print_r($days_inline_array);die;
        foreach($days_inline_array as $k=>$v){
            if($k==0){
                $line_time=$v;
            }else{
                $line_time.=','.$v;
            }
            $btime=strtotime($v);
            $etime=$btime+86400;

            $map['a.create_time'] = array('between',"$btime,$etime");   
            //考试次数   今天考了多少次
            $ex=M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->where($map)->count();
            $examination.=','. M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->where($map)->count();
            //及格次数  今天及格的次数
            $map1['a.is_pass']=1;
            $map1['a.create_time']=array('between',"$btime,$etime");  
            $pa=M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->where($map1)->count();
            //几个次数除以做的次数就是及格率
            $pas =$pa/$ex*100;  
            $pass.=','.$pas;

        }
        $examination=substr($examination, 1);
        
        $pass=substr($pass, 1);
        }else{
            //没有时间段搜索的话，默认显示今天的
            // $_REQUEST['name']?$map['b.name']=array('like',"%$_REQUEST[name]%"):'';
            $bbtime=date('Y-m-d',time());//今天
            $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
            //这个时间查询以前是日期查询 不是时间戳  要改一下
            $btime=strtotime($bbtime);
            $etime=strtotime($ebtime);
            $map['a.create_time'] = array('between',"$btime,$etime"); 
            //考试次数
            $examination=M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->where($map)->count();
            //及格次数
            $map['a.is_pass']=1;
            $pass_number=M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->where($map)->count();
            $pass=$pass_number/$examination*100;
            $line_time=date('Y-m-d',time());
        }

        $this->assign('examination',$examination);
        $this->assign('pass',$pass);
        $this->assign('line_time',$line_time);



        //习题统计   没有明确的习题记录，我要根据试卷做的时间去联合查询 习题表    试卷习题表    做试卷记录表
        // M('exercises a')->join('left join teach_practice_exercises b on a.id=b.exercises_id')->join('left join teach_user_test_log c on b.practice_id=c.practice_id')
        // M('exercises_all_log')->where($condition)
        $_REQUEST['question']?$condition['question']=array('like',"%$_REQUEST[question]%"):'';
        $this->assign('question',$_REQUEST['question']);
        $qstart_time = strtotime($_REQUEST['qstart_time']);
        $qend_time = strtotime($_REQUEST['qend_time']." 23:59:59");
        if(!empty($_REQUEST['qstart_time']) && !empty($_REQUEST['qend_time'])){
            //这个时间条件会被切割
            $this->assign('qstart_time',$_REQUEST['qstart_time']);
            $this->assign('qend_time',$_REQUEST['qend_time']);

                    //考试次数
        // M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->where($map)->count();

        //开始时间 ~ 结束时间
        $qstart_date=I('qstart_time');
        $qend_date=I('qend_time');
//
        //如果为空，则从今天的0点为开始时间
        if(!empty($qstart_date))
            $qstart_date=date('Y-m-d',strtotime($qstart_date));
        else
            $qstart_date=date('Y-m-d',time());
//
        //如果为空，则以明天的0点为结束时间（不存在24:00:00，只会有00:00:00）
        if(!empty($qend_date))
            $qend_date=date('Y-m-d',strtotime($qend_date));
        else
            $qend_date=date('Y-m-d',strtotime('+1 day'));

        //between 查询 要求必须是从低到高
        if($qstart_date>$qend_date)
        {
            $ttt=$qstart_date;
            $qstart_date=$qend_date;
            $qend_date=$ttt;
        }elseif($qstart_date==$qend_date){
            echo '时间输入错误';die;
        }
        $time_s=strtotime($qstart_date);
        $time_e=strtotime($qend_date);

        //生成中间时间点数组（时间戳格式、日期时间格式、日期序列）
        $days_inline_array=array();
        $times_inline_array=array();
        $days_list=array();
        //判断开始和结束时间是不是在同一天
        $days_inline_array[0]=$qstart_date;
        $times_inline_array[0]=$time_s;
        if(
            date('Y-m-d',$time_s)
            ==date('Y-m-d',$time_e)
        ){
            $days_inline_array[1]=$qend_date;
            $times_inline_array[1]=$time_e;
            $days_list[]=date('Y-m-d',$time_s);
        }
        else
        {
            /**
             * A.取开始时间的第二天凌晨0点
             * B.用结束时间减去A
             * C.用B除86400取商，取余
             * D.用A按C的商循环+86400，取得分割时间点，如果C没有余数，则最后一个时间点与循环最后一个时间点一致
             */
            $A_temp=date('Y-m-d',$time_s+86400);
            $A=strtotime($A_temp);
            $B=$time_e-$A;
            $C_quotient=floor($B/86400);    //商舍去法取整
            $C_remainder=fmod($B,86400);               //余数

            $days_inline_array[1]=$A_temp;
            $times_inline_array[1]=$A;
            $days_list[]=date('Y-m-d',$A);
            for($increase_time=$A,$c_count_t=1;$c_count_t<=$C_quotient;$c_count_t++)
            {
                $increase_time+=86400;
                $days_inline_array[]=date('Y-m-d',$increase_time);
                $times_inline_array[]=$increase_time;
                $days_list[]=date('Y-m-d',$increase_time);
            }
            //如果有余数，则最后一个时间点大于循环最后一个时间点，但在同一天
            if($C_remainder)
            {
                $days_inline_array[]=$qend_date;
                $times_inline_array[]=$time_e;
                //这里不再添加 $days_list，因为余数和最后一个凌晨0点必定是同一天
            }
        }

        // print_r($days_inline_array);die;
        foreach($days_inline_array as $k=>$v){
            if($k==0){
                $qline_time=$v;
            }else{
                $qline_time.=','.$v;
            }
            $btime=strtotime($v);
            $etime=$btime+86400;

            $condition['create_time'] = array('between',"$btime,$etime");  
            //做题次数
            $xt_count.=','.M('exercises_all_log')->where($condition)->count();

            //错题次数
            $con['is_true']=0;
            $con['create_time']=array('between',"$btime,$etime");  
            $ct_count.=','.M('exercises_all_log')->where($con)->count();
        }
            $xt_count=substr($xt_count, 1);
            $ct_count=substr($ct_count, 1);
        }else{
            //没有时间段搜索的话，默认显示今天的
            $bbtime=date('Y-m-d',time());//今天
            $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
            //这个时间查询以前是日期查询 不是时间戳  要改一下
            $btime=strtotime($bbtime);
            $etime=strtotime($ebtime);
            $condition['create_time'] = array('between',"$btime,$etime"); 
            //做题次数

            $xt_count=M('exercises_all_log')->where($condition)->count();
            // print_r($condition);die;
            //错题数
            $condition['is_true']=0;
            $ct_count=M('exercises_all_log')->where($condition)->count();
            $qline_time=date('Y-m-d',time());
        }
        //做题分析  我没有根据搜索条件来
        //正确率
        $where['is_true']=1;
        $true=M('exercises_all_log')->where($where)->count();
        $x_c=M('exercises_all_log')->count();
        $t_rate=$true/$x_c*100;//正确率
        $c_rate=100-$t_rate;//错误率
        $this->assign('t_rate',$t_rate);
        $this->assign('c_rate',$c_rate);
        $this->assign('xt_count',$xt_count);
        $this->assign('ct_count',$ct_count);
        $this->assign('qline_time',$qline_time);

        $this->assign('fail_rate',$fail_rate);
        $this->assign('error_rate',$error_rate);
        $this->assign('error_count',$error_count);
        $this->assign('make_rate',$make_rate);
        $this->assign('make_count',$make_count);
        $this->assign('exercises_count',$exercises_count);
        $this->assign('pass_rate',$pass_rate);
        $this->assign('pass_count',$pass_count);
        $this->assign('write_count',$write_count);
        $this->assign('cur','学习统计');
        $this->display();
    }
    //在线成绩
    public function online_results(){
        $iid=session('iid');

        // $_REQUEST['type']?$type=$_REQUEST['type']:$type=2;
        $_REQUEST['username']?$map['c.username']=array('like',"%$_REQUEST[username]%"):'';
        $_REQUEST['class_id']?$map['c.class_id']=$_REQUEST['class_id']:'';
        if($_REQUEST['start_time'] && $_REQUEST['end_time']){
            $btime=strtotime($_REQUEST['start_time']);
            $etime=strtotime($_REQUEST['end_time']." 23:59:59");
            $map['a.create_time']=array('between',"$btime,$etime");  
        }

        $map['b.type']=2;
        $count = M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where($map)->field('b.name,a.*,c.class_id,c.username')->group('practice_id')->count();
        // print_r($map);die;
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //试卷题记录表 我现在查出的是所有用户 
        $test_list=M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->field('b.name,a.*,c.class_id,c.username')->group('practice_id')->select();

        $show = $Page->show();// 分页显示输出
        $this->assign("_page", $show);

        // echo '<pre>';print_r($test_list);die;
        foreach($test_list as $k=>$v){
            // $test_list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $class_id=M('ucenter_member')->where("id=$v[uid]")->getField('class_id');
            if($class_id){
                $test_list[$k]['class_name']=M('class')->where("id=$class_id")->getField('name');
            }else{
                $test_list[$k]['class_name']='';
            }
            $test_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            //考试次数
            $m['b.type']=2;
            $m['a.uid']=$v['uid'];
            $m['a.practice_id']=$v['practice_id'];
            $test_list[$k]['count']=M('user_test_log a')->join('teach_practice b on a.practice_id=b.id')->where($m)->count();
            //分数先空着
            $f=M('user_test_log a')->join('teach_practice b on a.practice_id=b.id')->where($m)->field('a.score')->select();
            foreach($f as $kk=>$vv){
                $score[$kk]=$vv['score'];
            }
            // echo '<pre>';print_r($f);die;
            $test_list[$k]['max_score']=max($score);
            $jg_id=M('ucenter_member')->where("id=$v[uid]")->getField('iid');
            if($jg_id !== $iid){
                unset($test_list[$k]);
            }
        }
        // echo '<pre>';print_r($test_list);die;
        //练习册
        // $practice_list=M('user_test_log a')->join('teach_practice b on a.practice_id=b.id')->where('b.type=1')->field('b.name,a.*')->select();
        $condition['b.type']=1;
        $_REQUEST['username']?$condition['c.username']=array('like',"%$_REQUEST[username]%"):'';
        $_REQUEST['class_id']?$condition['c.class_id']=$_REQUEST['class_id']:'';
        if($_REQUEST['start_time'] && $_REQUEST['end_time']){
            $btime=strtotime($_REQUEST['start_time']);
            $etime=strtotime($_REQUEST['end_time']);
            $condition['a.create_time']=array('between',"$btime,$etime");  
        }
        $practice_list=M('user_test_log a')->join('left join teach_practice b on a.practice_id=b.id')->join('left join teach_ucenter_member c on a.uid = c.id')->where($condition)->field('b.name,a.*,c.class_id,c.username')->group('practice_id')->select();
        // echo '<pre>';print_r($test_list);die;
        foreach($practice_list as $k=>$v){
            $practice_list[$k]['username']=M('ucenter_member')->where("id=$v[uid]")->getField('username');
            $class_id=M('ucenter_member')->where("id=$v[uid]")->getField('class_id');
            if($class_id){
                $practice_list[$k]['class_name']=M('class')->where("id=$class_id")->getField('name');
            }else{
                $practice_list[$k]['class_name']='';
            }
            $practice_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            //考试次数
            $con['b.type']=1;
            $con['a.uid']=$v['uid'];
            $con['a.practice_id']=$v['practice_id'];
            $practice_list[$k]['count']=M('user_test_log a')->join('teach_practice b on a.practice_id=b.id')->where($con)->count();
            //分数先空着

            $jg_id=M('ucenter_member')->where("id=$v[uid]")->getField('iid');
            if($jg_id !== $iid){
                unset($practice_list[$k]);
            }
        }

        //班级列表
        $class_list=M('class')->where("iid=$iid")->select();
        $this->assign('class_list',$class_list);
        $this->assign('msg',$_REQUEST);
        $this->assign('test_list',$test_list);
        $this->assign('practice_list',$practice_list);
        $this->assign('cur','在线成绩');
        $this->display();
    }
    // 试卷  查看明细
    public function online_detail(){
        $id=$_REQUEST['id'];
        $map['practice_id']=M('user_test_log')->where("id=$id")->getField('practice_id');
        $map['uid']=M('user_test_log')->where("id=$id")->getField('uid');
        $list=M('user_test_log')->where($map)->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $list[$k]['sj_name']=M('practice')->where("id=$v[practice_id]")->getField('name');
        }
        $this->assign('cur','在线成绩');
        $this->assign('list',$list);
        $this->display();
    }

        // 试卷  查看明细
    public function practice_detail(){
        $id=$_REQUEST['id'];
        $map['practice_id']=M('user_test_log')->where("id=$id")->getField('practice_id');
        $map['uid']=M('user_test_log')->where("id=$id")->getField('uid');
        $list=M('user_test_log')->where($map)->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $list[$k]['sj_name']=M('practice')->where("id=$v[practice_id]")->getField('name');
        }
        $this->assign('cur','在线成绩');
        $this->assign('list',$list);
        $this->display();
    }
    //错题汇总
    public function wrong_questions(){
        $_REQUEST['question']?$map['b.question']=array('like',"%$_REQUEST[question]%"):'';
        if($_REQUEST['start_time'] && $_REQUEST['end_time']){
            $btime=strtotime($_REQUEST['start_time']);
            $etime=strtotime($_REQUEST['end_time']." 23:59:59");
            $map['b.create_time']=array('between',"$btime,$etime");  
        }
        $count = M('exercises_log a')->join("left join teach_exercises b on a.exercises_id=b.id")->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // print_r($map);die;
        $list=M('exercises_log a')->join("left join teach_exercises b on a.exercises_id=b.id")->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->field('a.*,b.question,b.style')->select();
        // print_r($list);die;
        foreach($list as $k=>$v){
            if($v['style']==1){
                $list[$k]['style']='单选';
            }else{
                $list[$k]['style']='多选';
            }
        }
        $show = $Page->show();// 分页显示输出
        $this->assign("msg", $_REQUEST);
        $this->assign("list", $list);
        $this->assign("_page", $show);
        $this->assign('cur','错题汇总');
        $this->display();
    }
}
